package com.wjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.enums.AppHttpCodeEnum;
import com.wjh.exception.SystemException;
import com.wjh.mapper.LogisticsStaffMapper;
import com.wjh.pojo.FlowerType;
import com.wjh.pojo.LogisticsStaff;
import com.wjh.service.LogisticsStaffService;
import com.wjh.utils.ResponseResult;
import com.wjh.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 物流人员表(LogisticsStaff)表服务实现类
 *
 * @author makejava
 * @since 2024-03-10 21:35:42
 */
@Service("logisticsStaffService")
public class LogisticsStaffServiceImpl extends ServiceImpl<LogisticsStaffMapper, LogisticsStaff> implements LogisticsStaffService {

    @Override
    public ResponseResult queryAll(Integer pageNum, Integer pageSize, String name, String phone) {

        LambdaQueryWrapper<LogisticsStaff> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,LogisticsStaff::getName,name);
        queryWrapper.like(phone!=null,LogisticsStaff::getPhone,phone);
        //分页查询所有
        Page<LogisticsStaff> certificatePage = new Page<>(pageNum,pageSize);
        Page<LogisticsStaff> page = page(certificatePage, queryWrapper);
        List<LogisticsStaff> records = page.getRecords();

        return ResponseResult.okResult(new PageVo(records,page.getTotal()));
    }

    @Override
    public ResponseResult updateStaff(LogisticsStaff logisticsStaff) {
        if(logisticsStaff.getId() == null){
            throw new SystemException(AppHttpCodeEnum.ID_NOTNULL);
        }
        if(!StringUtils.hasText(logisticsStaff.getName())){
            throw new SystemException(AppHttpCodeEnum.NOT_NAME);
        }
        if(!StringUtils.hasText(logisticsStaff.getPhone())){
            throw new SystemException(AppHttpCodeEnum.STAFF_PHONE_ERROR);
        }
        if(logisticsStaff.getAge() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_AGE_ERROR);
        }
        if(logisticsStaff.getSex() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_SEX_ERROR);
        }
        updateById(logisticsStaff);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delByIds(List<Integer> id) {
        if(id == null){
            throw new SystemException(AppHttpCodeEnum.ID_NOTNULL);
        }
        removeByIds(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult addStaff(LogisticsStaff logisticsStaff) {
        if(!StringUtils.hasText(logisticsStaff.getName())){
            throw new SystemException(AppHttpCodeEnum.NOT_NAME);
        }
        if(!StringUtils.hasText(logisticsStaff.getPhone())){
            throw new SystemException(AppHttpCodeEnum.STAFF_PHONE_ERROR);
        }
        if(logisticsStaff.getAge() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_AGE_ERROR);
        }
        if(logisticsStaff.getSex() == null){
            throw new SystemException(AppHttpCodeEnum.STAFF_SEX_ERROR);
        }
        save(logisticsStaff);
        return ResponseResult.okResult();
    }

    @Override
    public LogisticsStaff getStaffById(Integer id) {
        LogisticsStaff byId = getById(id);
        return byId;
    }
}
